package com.mall.controller;

import com.mall.dto.ActionResult;
import com.mall.po.Order;
import com.mall.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author mahaoqi
 * @ClassName OrderController
 * @Description TODO
 * @Date 2021/1/7 11:47
 * @Version 1.0
 **/
@RestController
@RequestMapping("/shangcheng")
@Api(tags = "订单相关操作")
public class OrderController {

    @Resource
    private OrderService orderService;


    /*
     * @Author mahaoqi
     * @Description 添加
     * @Date 2021/1/7 14:34
     * @Param [order]
     * @return com.mall.dto.ActionResult
     **/
    @ApiOperation(value = "添加订单信息")
    @ApiImplicitParam(name = "order",value = "没有orderid的对象（json）",required = true)
    @RequestMapping(value = "/orderadd",method = RequestMethod.POST)
    public ActionResult orderadd(@RequestBody Order order){
        if(orderService.addOrder(order)){
            return new ActionResult(200,"添加成功",null);
        }
        return new ActionResult(500,"添加失败",null);
    }

    /*
     * @Author mahaoqi
     * @Description 删除
     * @Date 2021/1/7 14:34
     * @Param [orderid]
     * @return com.mall.dto.ActionResult
     **/
    @ApiOperation(value = "删除订单")
    @ApiImplicitParam(name = "orderid",value = "要删除目标订单的id",required = true)
    @RequestMapping(value = "/orderdel",method = RequestMethod.DELETE)
    public ActionResult orderdel(Integer orderid){
        if(orderService.removeById(orderid)){
            return new ActionResult(200,"删除成功",null);
        }
        return new ActionResult(500,"删除失败",null);
    }

    /*
     * @Author mahaoqi
     * @Description 修改
     * @Date 2021/1/7 14:34
     * @Param [order]
     * @return com.mall.dto.ActionResult
     **/
    @ApiOperation(value = "更新订单内容")
    @ApiImplicitParam(name = "order",value = "更改后的内容（不能更改orderid）",required = true)
    @RequestMapping(value = "/orderupdate",method = RequestMethod.PUT)
    public ActionResult orderupdate(@RequestBody Order order){
        Boolean flag = orderService.modifyOrder(order);
        if(flag!=null){
            if(flag){
                return new ActionResult(200,"修改成功",null);
            }else{
                return new ActionResult(500,"修改失败",null);
            }
        }else{
            return new ActionResult(500,"资源未找到",null);
        }
    }


    /*
     * @Author mahaoqi
     * @Description 查找
     * @Date 2021/1/7 14:40
     * @Param [orderid]
     * @return com.mall.dto.ActionResult
     **/
    @ApiOperation(value = "查询单个订单")
    @ApiImplicitParam(name = "orderid",value = "orderid",required = true,dataType = "Integer")
    @RequestMapping(value = "/orderquery",method = RequestMethod.GET)
    public ActionResult orderquery(Integer orderid){
        Order order = orderService.findByid(orderid);
        if(order!=null){
            return new ActionResult(200,"查询成功",order);
        }
        return new ActionResult(500,"资源未找到",null);
    }




    @ApiOperation(value = "分页获取vip信息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "查询的页码",required = true ),
            @ApiImplicitParam(name = "pagesize",value = "每一页包含数据条数",required = true)
    })
    @RequestMapping(value = "/oderlist",method = RequestMethod.GET)
    public ActionResult oderlist(Integer page,Integer pagesize){
        Map result = new HashMap();
        Integer count = orderService.findCount();
        List<Order> orders = orderService.findList((page-1)*pagesize,pagesize);
        if(orders!=null){
            result.put("count",count);
            result.put("data",orders);
            return new ActionResult(200,"查询成功",result);
        }
        return new ActionResult(500,"资源未找到",null);
    }

    /*
     * @Author mahaoqi
     * @Description 批量删除
     * @Date 2021/1/7 15:47
     * @Param [vipids]
     * @return com.mall.dto.ActionResult
     **/
    @ApiOperation(value = "批量删除订单")
    @ApiImplicitParam(name = "orderids",value = "要删除的orderids数组",required = true)
    @RequestMapping(value = "/orderdelbatch",method = RequestMethod.DELETE)
    public ActionResult orderdelList(@RequestParam("orderids") Integer[] orderids){
        if(orderService.removeBatch(orderids)){
            return new ActionResult(200,"删除成功",null);
        }
        return new ActionResult(500,"删除失败",null);
    }
}
